package com.alex.keywordtool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.regex.Pattern;

public class ParseAdultKewords {

	private static Collection<Collection<String>> keywords = new ArrayList<Collection<String>>();
	
	private static Collection<String> results = new LinkedHashSet<String>();

	public static void main(String[] args) throws Exception {
		addKeyword("fuck");
		addKeyword("milf");
		addKeyword("porn");
		addKeyword("hardcore");
		addKeyword("tits");
		addKeyword("cumshot");
		addKeyword("masturbate");
		addKeyword("lesbian");
		addKeyword("voyeur");
		addKeyword("cunnilingus");
		addKeyword("sex");
		addKeyword("nude");
		addKeyword("hentai");
		addKeyword("yaoi");
		addKeyword("gay");
		addKeywords(new String[] { "hot", "teen" });
		addKeywords(new String[] { "cam", "teen" });
		addKeywords(new String[] { "hot", "cam" });
		addKeywords(new String[] { "webcam", "teen" });
		addKeywords(new String[] { "webcam", "hot" });

		
		processFile("/home/alex/Downloads/Postuhov/5/KeywordsAndDatas.txt");
		
		BufferedWriter writer = new BufferedWriter(new FileWriter("/home/alex/Desktop/adult.txt"));
		for (String keyword : results) {
			writer.write(keyword + "\n");
		}
		writer.close();
	}

	private static void processFile(String file) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file), 0x1000000);
		String line = null;

		int n = 0;
		while ((line = reader.readLine()) != null) {
			if (line.isEmpty()) {
				continue;
			}
			Keyword keywordInfo = Keyword.parse(line);
			if (matches(keywordInfo)/* && keywordInfo.getFloatAttr(2) > 0.1*/) {
				results.add(keywordInfo.getKeyword()/* + "\t" + keywordInfo.getAttrs()[2]*/);
			}
			n++;

			if (n % 100000 == 0) {
				System.out.println(n + " - " + results.size());
			}
		}
		reader.close();
	}

	private static void addKeywords(String[] k) {
		keywords.add(Arrays.asList(k));
	}

	private static void addKeyword(String keyword) {
		keyword = pattern.matcher(keyword).replaceAll("");
		keywords.add(Collections.singleton(keyword));
	}

	private static Pattern pattern = Pattern.compile("[^a-zA-Z ]");

	private static boolean matches(Keyword keywordInfo ) {
		for (Collection<String> group : keywords) {
			boolean f = true;
			for (String keyword : group) {
				if (!keywordInfo.getWords().contains(keyword)) {
					f = false;
					break;
				}
			}
			if (f) {
				return true;
			}
		}
		return false;
	}
}

